package mapper;

import osc.OSCMessage;

/**
 * @author gt218330
 * 
 */
public class OSC2MIDIBinding {

	private int myOSCParameter = 0;
	private int myMIDIParameter = 0;

	private boolean isRanged = false;

	private float myOSC = 0;
	private int myMIDI = 0;

	private float minOSC = 0;
	private float maxOSC = 0;

	private int minMIDI = 0;
	private int maxMIDI = 0;

	private float rangeStep = 0;

	public OSC2MIDIBinding() {

	}

	public OSC2MIDIBinding(int myOSCParameterValue, int myMIDIParameterValue, int myOSCValue, int myMIDIValue) {
		this.isRanged = false;
		this.myOSC = myOSCValue;
		this.myMIDI = myMIDIValue;
		this.myOSCParameter = myOSCParameterValue;
		this.myMIDIParameter = myMIDIParameterValue;
	}

	public OSC2MIDIBinding(int myOSCParameterValue, int myMIDIParameterValue, float minOSCValue, float maxOSCValue, int minMIDIValue, int maxMIDIValue) {
		this.isRanged = true;
		this.myOSCParameter = myOSCParameterValue;
		this.myMIDIParameter = myMIDIParameterValue;

		this.minOSC = minOSCValue;
		this.maxOSC = maxOSCValue;

		this.minMIDI = minMIDIValue;
		this.maxMIDI = maxMIDIValue;

		updateRangeStep();
	}

	private void updateRangeStep() {
		if (isRanged) {
			this.rangeStep = (this.maxMIDI - this.minMIDI) / (this.maxOSC - this.minOSC);
		}
	}

	public int[] translate(OSCMessage message) {
		int translation[] = new int[2];
		if (this.isRanged) {
			translation[0] = this.myMIDIParameter;
			translation[1] = (int) (((Float) message.getParameter(myOSCParameter)) * rangeStep + this.minMIDI);
		} else {
			translation[0] = this.myMIDIParameter;
			translation[1] = this.myMIDI;
		}
		if (this.minOSC < 0) {
			translation[1] += 63;
		}
		// translation[1] = Math.abs(translation[1]);
		// if (translation[1] > 127) {
		// translation[1] = 127;
		// }
		// if (translation[1] < 1) {
		// translation[1] = 1;
		// }
		System.out.println("Translation = " + translation[0] + " " + translation[1]);
		return translation;
	}

	public int getMyOSCParameter() {
		return myOSCParameter;
	}

	public void setMyOSCParameter(int myOSCParameterValue) {
		myOSCParameter = myOSCParameterValue;
	}

	public int getMyMIDIParameter() {
		return myMIDIParameter;
	}

	public void setMyMIDIParameter(int myMIDIParameterValue) {
		myMIDIParameter = myMIDIParameterValue;
	}

	public float getMyOSC() {
		return myOSC;
	}

	public void setMyOSC(float myOSCValue) {
		myOSC = myOSCValue;
	}

	public int getMyMIDI() {
		return myMIDI;
	}

	public void setMyMIDI(int myMIDIValue) {
		myMIDI = myMIDIValue;
	}

	public int getOSCParameter() {
		return myOSCParameter;
	}

	public void setOSCParameter(int myOSCParameterValue) {
		myOSCParameter = myOSCParameterValue;
	}

	public int getMIDIParameter() {
		return myMIDIParameter;
	}

	public void setMIDIParameter(int myMIDIParameterValue) {
		myMIDIParameter = myMIDIParameterValue;
	}

	public boolean isRanged() {
		return isRanged;
	}

	public void setRanged(boolean isRangedValue) {
		isRanged = isRangedValue;
	}

	public float getMinOSC() {
		return minOSC;
	}

	public void setMinOSC(float minOSCValue) {
		minOSC = minOSCValue;
		updateRangeStep();
	}

	public float getMaxOSC() {
		return maxOSC;
	}

	public void setMaxOSC(float maxOSCValue) {
		maxOSC = maxOSCValue;
		updateRangeStep();
	}

	public int getMinMIDI() {
		return minMIDI;
	}

	public void setMinMIDI(int minMIDIValue) {
		minMIDI = minMIDIValue;
		updateRangeStep();
	}

	public int getMaxMIDI() {
		return maxMIDI;
	}

	public void setMaxMIDI(int maxMIDIValue) {
		maxMIDI = maxMIDIValue;
		updateRangeStep();
	}

}
